package com.dadaguai.controller;

import com.dadaguai.annotation.JwtToken;
import com.dadaguai.common.BusinessException;
import com.dadaguai.common.R;
import com.dadaguai.common.validator.LoginGroup;
import com.dadaguai.common.validator.RegisterGroup;
import com.dadaguai.constants.ResponseCode;
import com.dadaguai.domain.UserInfo;
import com.dadaguai.dto.UserInfoDto;
import com.dadaguai.service.EmailCodeService;
import com.dadaguai.service.UserInfoService;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@RestController
@Validated
@RequestMapping("/user")
public class UserController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Resource
    UserInfoService userInfoService;

    @Resource
    EmailCodeService emailCodeService;


    /**
     * 验证码登录/注册
     *
     * @param user
     * @return
     * @throws BusinessException
     */
    @PostMapping("/register")
    public R register(@Validated(RegisterGroup.class) @RequestBody UserInfoDto user, HttpSession session) throws BusinessException {
        return userInfoService.register(user, session);
    }

    /**
     * 用户登录
     *
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@Validated(LoginGroup.class) @RequestBody UserInfoDto user, HttpSession session) {
        if (user == null) return R.error("请输入正确的账号密码", ResponseCode.USER_PARAM_ERROR);
//        登陆时校验图片验证码
//        R r = emailCodeService.checkCode(user.getCode(), CaptchaConstants.IMG_CODE, user.getEmail());
//        if (r.getState().equals("error")) return r;
        return userInfoService.loginCheck(user, session, "1");
    }

    /**
     * 退出登录
     */
    @GetMapping("/logout")
    public void logout(HttpSession session) {
        // 清除session
        session.invalidate();
    }

    /**
     * 根据id获取用户信息
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/userInfo/{id}")
    public R getUserInfo(@PathVariable String id) {

        return userInfoService.getUserInfo(id);

    }

    /**
     * 修改密码
     *
     * @param user
     * @return
     */
    @JwtToken
    @PostMapping("/changePassword")
    public R changePassword(@Validated(LoginGroup.class) @RequestBody UserInfoDto user) {
        return userInfoService.changePassword(user.getEmail(), user.getPassword(), user.getCode());
    }

    /**
     * 检查邮箱是否注册
     *
     * @param email
     * @return
     */
    @GetMapping("/checkEmail")
    public R checkEmail(@RequestParam String email) {
        Integer count = emailCodeService.queryEmailRegister(email);
        if (count != 0) {
            return R.success("");
        }
        return R.error("该邮箱尚未注册");
    }

    /**
     * 修改用户资料
     *
     * @param user
     * @param session
     * @return
     */
    @JwtToken
    @PostMapping("/profile")
    public R setProfile(@RequestBody UserInfoDto user, HttpSession session) {
        if (user == null) return R.error("参数错误", ResponseCode.PARAM_MISS_ERROR);
        return userInfoService.setProfile(user,session);
    }

}
